package com.example.petshopping.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petshopping.R;
import com.example.petshopping.model.Product;

import java.util.List;

/**
 * 鱼缸造景悬赏适配器
 */
public class FishTankProductAdapter extends RecyclerView.Adapter<FishTankProductAdapter.ViewHolder> {

    private final Context context;
    private final List<Product> products;
    private OnItemClickListener listener;

    public FishTankProductAdapter(Context context, List<Product> products) {
        this.context = context;
        this.products = products;
    }

    public interface OnItemClickListener {
        void onItemClick(Product product, int position);
        void onCloseClick(Product product, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_fish_tank_product, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Product product = products.get(position);

        // 设置商品图片（所有商品都使用yugang.png）
        holder.imgProduct.setImageResource(R.drawable.yugang);
        
        // 设置商品标题
        holder.tvProductTitle.setText(product.getName());
        
        // 设置商品价格
        holder.tvProductPrice.setText(product.getPrice());
        
        // 设置店铺名称
        holder.tvShopName.setText(product.getSellerName());
        
        // 商品点击事件
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(product, position);
            }
        });
        
        // 关闭按钮点击事件
        holder.btnClose.setOnClickListener(v -> {
            if (listener != null) {
                listener.onCloseClick(product, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return products.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imgProduct;
        TextView tvProductTitle;
        TextView tvProductPrice;
        ImageView imgShopAvatar;
        TextView tvShopName;
        TextView btnClose;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imgProduct = itemView.findViewById(R.id.img_product);
            tvProductTitle = itemView.findViewById(R.id.tv_product_title);
            tvProductPrice = itemView.findViewById(R.id.tv_product_price);
            imgShopAvatar = itemView.findViewById(R.id.img_shop_avatar);
            tvShopName = itemView.findViewById(R.id.tv_shop_name);
            btnClose = itemView.findViewById(R.id.btn_close);
        }
    }
} 